﻿using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using Axis.Dao.Research;
using Axis.BusinessObject.Research;
using Axis.Web.Administrator.Global;
using System.Web.Script.Serialization;
using Axis.BusinessObject.Core;
using System.Web.SessionState;
using System.Xml;
using System.Text;
using Axis.Dao.Core;
using System;

namespace Axis.Web.Administrator.Views.Gift
{
    public partial class ExportExcel : System.Web.UI.Page
    {
        GridView GridView1 = new GridView();
        protected void Page_Load(object sender, EventArgs e)
        {
           // string Context = "  Context=\"MemberListGift\" ";
           //// if (Request.QueryString["Completed"].ToString() == "1")
           // Context = "  Context=\"MemberListGift\" ";
           // string InputValue = string.Format("<InputValue RowsPerPage=\"{0}\" PageIndex=\"{1}\" {2}/>", 1000000, 1, Context);
           // GridView1.DataSource = (new CResearchDao()).GetDataExport(InputValue);
           // GridView1.DataBind();
            string projectID = "0";
            if (Request.QueryString["ProjectID"] != null)
                projectID = Request.QueryString["ProjectID"].ToString();
            string giftID = "";
            if (Request.QueryString["GiftID"] != null)
                giftID = Request.QueryString["GiftID"].ToString();
            string giftReceived = "";
            if (Request.QueryString["GiftReveiced"] != null)
                giftReceived = Request.QueryString["GiftReveiced"].ToString();
            string fromDate = "";
            if (Request.QueryString["FromDate"] != null)
                fromDate = Request.QueryString["FromDate"].ToString();
            string toDate = "";
            if (Request.QueryString["ToDate"] != null)
                toDate = Request.QueryString["ToDate"].ToString();
            string fromMark = "";
            if (Request.QueryString["FromMark"] != null)
                fromMark = Request.QueryString["FromMark"].ToString();
            string toMark = "";
            if (Request.QueryString["toMark"] != null)
                toMark = Request.QueryString["toMark"].ToString();


            XmlDocument doc = new XmlDocument();
            XmlElement root = doc.CreateElement("InputValue");
            doc.AppendChild(root);
            root.SetAttribute("UserID", CSecurity.USER_ID.ToString());
            root.SetAttribute("ProjectID", projectID);
            root.SetAttribute("GiftID", giftID);
            root.SetAttribute("GiftReveiced", giftReceived);
            root.SetAttribute("SecrectToken", "");
            root.SetAttribute("Context", "MemberListGift");
            if (fromDate.Length == 10)
            {
                string temp = string.Format("{0}-{1}-{2}", fromDate.Substring(6, 4), fromDate.Substring(3, 2), fromDate.Substring(0, 2));
                fromDate = temp;
            }
            else
                fromDate = "";
            if (toDate.Length == 10)
            {
                string temp2 = string.Format("{0}-{1}-{2}", toDate.Substring(6, 4), toDate.Substring(3, 2), toDate.Substring(0, 2));
                toDate = temp2;
            }
            else
                toDate = "";

            root.SetAttribute("FromDate", fromDate);
            root.SetAttribute("ToDate", toDate);
            root.SetAttribute("FromMark", fromMark);
            root.SetAttribute("ToMark", toMark);
            GridView1.DataSource = (new CResearchDao()).GetDataExport(root.OuterXml);


            Export();
        }
        protected void Export()
        {
            Response.Clear();
            Response.Buffer = true;
            string filename = "MemberGift";
           
            filename += "_" + DateTime.Now.ToString();
            Response.AddHeader("content-disposition", string.Format("attachment;filename={0}.xls", filename));


            GridView1.AllowPaging = false;
            GridView1.DataBind();

            //Change the Header Row back to white color
            GridView1.HeaderRow.Style.Add("background-color", "#FFFFFF");

            //Apply style to Individual Cells
            GridView1.HeaderRow.Cells[0].Style.Add("background-color", "green");
            GridView1.HeaderRow.Cells[1].Style.Add("background-color", "green");
            GridView1.HeaderRow.Cells[2].Style.Add("background-color", "green");
            GridView1.HeaderRow.Cells[3].Style.Add("background-color", "green");

            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                GridViewRow row = GridView1.Rows[i];

                //Change Color back to white
                row.BackColor = System.Drawing.Color.White;

                //Apply text style to each Row
                row.Attributes.Add("class", "textmode");

                //Apply style to Individual Cells of Alternating Row
                if (i % 2 != 0)
                {
                    row.Cells[0].Style.Add("background-color", "#C2D69B");
                    row.Cells[1].Style.Add("background-color", "#C2D69B");
                    row.Cells[2].Style.Add("background-color", "#C2D69B");
                    row.Cells[3].Style.Add("background-color", "#C2D69B");
                }
            }
            // GridView1.RenderControl(hw);



            Response.ContentEncoding = System.Text.Encoding.Unicode;
            Response.BinaryWrite(System.Text.Encoding.Unicode.GetPreamble());

            System.IO.StringWriter sw = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter hw = new HtmlTextWriter(sw);

            GridView1.RenderControl(hw);

            Response.Write(sw.ToString());
            Response.Flush();
            Response.End();



        }
    }
}